/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * created on : 2012-07-19
 * author : yanguoyue
 */

#include "comm/napi/util/comm_function.h"
#include "comm/napi/util/napi_common.h"

#include "mars/baseevent/base_logic.h"
#include "mars/baseevent/baseevent.h"
#include "com_ohos_mars_BaseEvent.h"
//DEFINE_FIND_CLASS(KBaseEventJava2C, "com/tencent/mars/BaseEvent");
#include "hilog/log.h"
#define LOG_DOMAIN 0x0201
#define LOG_TAG "MY_TAG"
namespace mars {
namespace baseevent {

	std::vector<std::string>* getLoadModuleVec(){
		static std::vector<std::string> sg_modules;
		return &sg_modules;
	}

	void addLoadModule(std::string _module_name) {
		getLoadModuleVec()->push_back(_module_name);
	}
	//Register Array class in ETS //ETS to be modified
	napi_value getLoadLibraries(napi_env env) {
		napi_value arrayValue  = JS_CallStaticMethodByName(env, "Array", "constructor", 0);

		napi_value addMethodValue = JS_FindMethodByName(env, arrayValue, "push");
		if (addMethodValue == nullptr) {
			return nullptr;
		}
		for(std::vector<std::string>::iterator iter = getLoadModuleVec()->begin(); iter != getLoadModuleVec()->end(); ++iter){
			napi_value elementValue;
			napi_status status = napi_create_string_utf8(env,
                                    iter->c_str(),
                                    iter->size(),
                                    &elementValue);
			if (status != napi_ok) {
				return nullptr;
			}
			JS_CallMethodByName(env, arrayValue, addMethodValue, 1, elementValue);
		}

		return arrayValue;
	}
}
}

using namespace mars::baseevent;

extern "C" {

//JNIEXPORT void JNICALL Java_com_ohos_mars_BaseEvent_onCreate(JNIEnv* env, jclass)
napi_value JS_com_ohos_mars_BaseEvent_onCreate(napi_env env, napi_callback_info info)
{
    mars::baseevent::OnCreate();
	napi_value result;
	napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_BaseEvent_onInitConfigBeforeOnCreate(JNIEnv* env, jclass, jint _packer_encoder_version)
napi_value JS_com_ohos_mars_BaseEvent_onInitConfigBeforeOnCreate(napi_env env, napi_callback_info info)
{
	size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value anArg;
    napi_value *args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));    
    
    napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, anArg, &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");
	int packer_encoder_version;
	if (napi_ok != napi_get_value_int32(env, anArg, &packer_encoder_version)) {
		napi_create_int32(env, -1, &result);
    	return result;
	}
	mars::baseevent::OnInitBeforeOnCreate(packer_encoder_version);
	napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_BaseEvent_onDestroy(JNIEnv* env, jclass)
napi_value JS_com_ohos_mars_BaseEvent_onDestroy(napi_env env, napi_callback_info info)
{
    mars::baseevent::OnDestroy();
	napi_value result;
	napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_BaseEvent_onForeground (JNIEnv *, jclass, jboolean _isforeground) 
napi_value JS_com_ohos_mars_BaseEvent_onForeground(napi_env env, napi_callback_info info)
{
	size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value anArg;
    napi_value *args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));    
    
    napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, anArg, &valueType));
	NAPI_ASSERT(env, valueType == napi_boolean, "Wrong argument type. Boolean expected.");
	bool isforeground;
	if (napi_ok != napi_get_value_bool(env, anArg, &isforeground)) {
		napi_create_int32(env, -1, &result);
    	return result;
	}	
    mars::baseevent::OnForeground(isforeground);
	napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_BaseEvent_onNetworkChange (JNIEnv *, jclass) 
napi_value JS_com_ohos_mars_BaseEvent_onNetworkChange(napi_env env, napi_callback_info info)
{
	mars::baseevent::OnNetworkChange();
	napi_value result;
	napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_BaseEvent_onSingalCrash(JNIEnv *, jclass, jint _sig)
napi_value JS_com_ohos_mars_BaseEvent_onSingalCrash(napi_env env, napi_callback_info info)
{
	size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value anArg;
    napi_value *args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));    
    
    napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, anArg, &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");
	int sig;
	if (napi_ok != napi_get_value_int32(env, anArg, &sig)) {
		napi_create_int32(env, -1, &result);
    	return result;
	}
		
    mars::baseevent::OnSingalCrash(sig);
	napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_BaseEvent_onExceptionCrash(JNIEnv*, jclass)
napi_value JS_com_ohos_mars_BaseEvent_onExceptionCrash(napi_env env, napi_callback_info info)
{
    mars::baseevent::OnExceptionCrash();
	napi_value result;
	napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_comm_Alarm_onAlarm(JNIEnv * env, jclass, jlong _id)
napi_value JS_com_ohos_mars_comm_Alarm_onAlarm(napi_env env, napi_callback_info info)
{
	size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value anArg;
    napi_value *args = &anArg;
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));    
    
    napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, anArg, &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");
	int64_t id;
	if (napi_ok != napi_get_value_int64(env, anArg, &id)) {
		napi_create_int32(env, -1, &result);
    	return result;
	}
    
    mars::baseevent::OnAlarm((int64_t)id);
	napi_create_int32(env, 0, &result);
	return result;
}

}

void ExportBaseEvent() {
}
